/*
 * @Author: @yzcheng
 * @Date: 2021-04-23 18:31:52
 * @Version: 1.0
 * @LastEditors: @yzcheng
 * @Description:
 * @LastEditTime: 2021-06-23 17:23:06
 */
import SvgIcon from '@/components/SvgIcon/index.vue'
import hasPermission from '@/directive/has.js'
import { Langs } from '@/enums/langs'
import * as dayjs from '@/filters/day'
import i18n from '@/i18n/index.js'
import ElementPlus from 'element-plus'
import moment from 'moment'
import { ObjectType } from 'typings/interfaces'
import { App as VueApp, createApp as createVueApp } from 'vue'
import App from './App.vue'
import * as filters from './filters'
import globalMixin from './mixins/global.js'
import router from './router/index'
import { key, store } from './store'
// import { Router } from 'vue-router'

/**
 * 创建Vue实例
 * @param {string} el 挂载节点
 * @param {Object} micro 微应用配置
 * @param {'zh-cn'| 'en'} lang 多语言配置
 * @returns Vue 实例
 */

export default async function createApp(
  el: string,
  micro?: ObjectType,
  lang?: Langs
): Promise<VueApp> {
  const app = createVueApp(App)
  // 添加全局属性
  app.config.globalProperties = {
    $micro: micro, // 微应用
    $moment: moment,
    $filters: filters,
    $dayjs: dayjs, // 日期
  }
  if (lang) {
    i18n.global.locale = lang
  }
  const routers: any = await router

 
  routers.beforeEach((to, from, next) => {
    const passRoutes = ['/login','/outLogin']

    if (passRoutes.includes(to.path)) {
      
      next()
   
    } else {
      // 非登录页 鉴权
      if (!localStorage.getItem('token')) {
        // 判断 角色是否可以访问该路由
        next('/login')
      }else{
        next()
      }
    }
  })

  app
    .use(hasPermission)
    .use(store)
    .use(routers)
    .use(i18n)
    .use(store, key)
    .use(ElementPlus, {
      i18n: i18n.global.t,
      size: 'small', // set element-ui default size
    })
    .mixin(globalMixin)
    .component('svg-icon', SvgIcon)
    .mount(el)
  return app
}
